﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestUi : BaseUi
{
	string[] config = {
		"设置x",
		"设置y",
		"设置z",
		"生成地图",
	};
	public Transform parent;
	private void OnEnable()
	{
		Transform childOne = parent.GetChild(0);
		for (int i = 0; i < config.Length; i++)
		{
			if (parent.childCount < i + 1)
			{
				Transform newChild = Instantiate(childOne);
				newChild.SetParent(parent, false);
			}
			Transform target = parent.GetChild(i);
			target.GetComponentInChildren<Text>().text = config[i];
			target.GetChild(0).name = config[i];
			RefreshText(config[i], target.GetChild(0).gameObject);
		}
	}
	public override void _onClick(string btnTag, GameObject obj)
	{
		string str = obj.transform.parent.GetComponentInChildren<InputField>().text;
		float f = 0;
		try
		{
			if (!string.IsNullOrEmpty(str))
			{
				f = float.Parse(str);
			}
		}
		catch (System.Exception)
		{
			Debug.LogError(str);
		}
		switch (btnTag)
		{
			case "close":
				closeUi(gameObject);
				break;
			case "设置x":
				Global.gameData.setX = (int)f;
				break;
			case "设置y":
				Global.gameData.setY = (int)f;
				break;
			case "设置z":
				Global.gameData.setZ = (int)f;
				break;
			case "生成地图":
				Global.GameProxy.ControlBox.onSetSize(Global.gameData.setX, Global.gameData.setY, Global.gameData.setZ);
				break;
			default:
				Debug.Log("点击了" + btnTag + "未处理");
				break;
		}
		OnEnable();
	}
	public void RefreshText(string btnTag, GameObject obj)
	{
		string str = "";
		switch (btnTag)
		{
			case "设置x":
				str = $"设置x:{ Global.gameData.setX}";
				break;
			case "设置y":
				str = $"设置y:{ Global.gameData.setY}";
				break;
			case "设置z":
				str = $"设置z:{ Global.gameData.setZ}";
				break;
		}
		if (!string.IsNullOrEmpty(str))
		{
			obj.GetComponentInChildren<Text>().text = str;
		}
	}

}